/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.moedik.controller;

import com.moedik.classes.City;
import com.moedik.classes.Pool;
import com.moedik.classes.Shipping;
import com.moedik.classes.ShippingQos;
import com.moedik.classes.User;
import com.moedik.model.ShippingModel;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author SAMMY
 */
public class ShippingController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        ShippingModel shippingModel = new ShippingModel();
        if (request.getParameter("action").equals("manage")) {
            request.setAttribute("title", "Manage Shippings");
            if (((User) request.getSession().getAttribute("user")) == null) {
                response.sendRedirect("./users?action=login");
            } else if (((User) request.getSession().getAttribute("user")).getRoleId() == 3){
                ArrayList<Shipping> shippings = shippingModel.getAll();

                request.setAttribute("shippings", shippings);
                request.getRequestDispatcher("shippings/employee_manage.jsp").forward(request, response);
            } else if (((User) request.getSession().getAttribute("user")).getRoleId() == 2){
                if (((User) request.getSession().getAttribute("user")).getRoleId() != 2) {
                    request.getSession().setAttribute("flash", "Silahkan login sebagai manager");
                    response.sendRedirect("./users?action=login");
                } else if (request.getParameter("get") != null) {
                    response.setContentType("text/plain");
                    PrintWriter out = response.getWriter();
                    if (request.getParameter("get").equals("pool")) {
                        out.println("<option class=\"defPool\" >Pilih Pool Keberangkatan</option>");
                        int cityId = Integer.parseInt(request.getParameter("city"));
                        ArrayList<Pool> pools = shippingModel.getPoolServices(cityId);
                        Iterator itr = pools.iterator();
                        while (itr.hasNext()) {
                            Pool pol = (Pool) itr.next();
                            out.println("<option value=\"" + pol.getId() + "\">" + pol.getName() + "</option>");
                        }
                    } else if (request.getParameter("get").equals("shipping")) {
                        response.setContentType("text/html;charset=UTF-8");
                        int poolFrom = Integer.parseInt(request.getParameter("ori"));
                        int poolTo = Integer.parseInt(request.getParameter("dest"));
                        ArrayList<Shipping> services = shippingModel.getServices(poolFrom,poolTo);
                        request.setAttribute("services", services);
                        request.getRequestDispatcher("shippings/manage_list.jsp").forward(request, response);
                    }
                } else if (request.getParameter("mode") != null) {
                    if (request.getParameter("mode").equals("delete")) {
                        int serviceId = Integer.parseInt(request.getParameter("service"));
                        shippingModel.deleteService(serviceId);
                    } else if (request.getParameter("mode").equals("edit")) {
                        if (request.getMethod().equals("POST")) {
                            int serviceId = Integer.parseInt(request.getParameter("id"));
                            int poolFrom = Integer.parseInt(request.getParameter("poolFrom"));
                            int poolTo = Integer.parseInt(request.getParameter("poolTo"));
                            int qosId = Integer.parseInt(request.getParameter("qos"));
                            int cost = Integer.parseInt(request.getParameter("cost"));
                            shippingModel.updateService(serviceId, poolFrom, poolTo, qosId,cost);
                            request.getSession().setAttribute("flash", "Layanan paket berhasil diedit");
                        }
                        int serviceId = Integer.parseInt(request.getParameter("service"));
                        Shipping shipping = shippingModel.getShipping(serviceId);
                        ArrayList<ShippingQos> qos = shippingModel.getShippingQos();
                        ArrayList<City> cities = shippingModel.getCityServices();
                        ArrayList<Pool> poolsFrom = shippingModel.getPoolsFrom(serviceId);
                        ArrayList<Pool> poolsTo = shippingModel.getPoolsTo(serviceId);
                        request.setAttribute("cities", cities);
                        request.setAttribute("shipping", shipping);
                        request.setAttribute("poolsFrom", poolsFrom);
                        request.setAttribute("poolsTo", poolsTo);
                        request.setAttribute("qos", qos);
                        request.getRequestDispatcher("shippings/manage_edit.jsp").forward(request, response);
                    } else if (request.getParameter("mode").equals("add")) {
                        if (request.getMethod().equals("POST")) {
                            int poolFrom = Integer.parseInt(request.getParameter("poolFrom"));
                            int poolTo = Integer.parseInt(request.getParameter("poolTo"));
                            int qosId = Integer.parseInt(request.getParameter("qos"));
                            int cost = Integer.parseInt(request.getParameter("cost"));
                            shippingModel.addService(poolFrom, poolTo, qosId, cost);
                            request.getSession().setAttribute("flash", "Layanan Shipping berhasil ditambahkan");
                        }
                        ArrayList<City> cities = shippingModel.getCityServices();
                        ArrayList<ShippingQos> qos = shippingModel.getShippingQos();
                        request.setAttribute("cities", cities);
                        request.setAttribute("qos", qos);
                        request.getRequestDispatcher("shippings/manage_add.jsp").forward(request, response);
                    }
                } else {
                    ArrayList<City> cities = shippingModel.getCityServices();
                    request.setAttribute("cities", cities);
                    request.getRequestDispatcher("shippings/manage.jsp").forward(request, response);
                }
            } 
        } else if (request.getParameter("action").equals("delete")) {
            if (((User) request.getSession().getAttribute("user")).getRoleId() > 3) {
                response.sendError(403);
            } else {
                int id = Integer.parseInt(request.getParameter("id"));

                if (shippingModel.delete(id) == 0) {
                    response.setStatus(404);
                }
            }
        } else if (request.getParameter("action").equals("report")) {
            if (((User) request.getSession().getAttribute("user")) == null) {
                response.sendRedirect("./users?action=login");
            }else if (((User) request.getSession().getAttribute("user")).getRoleId() != 2) {
                request.getSession().setAttribute("flash", "Silahkan login sebagai manager");
                response.sendRedirect("./users?action=login");
            } else if(request.getMethod().equals("POST")){
                int year = Integer.parseInt(request.getParameter("year"));
                ArrayList<Integer> count = shippingModel.getReport(year);
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.YEAR, year);
                request.setAttribute("count", count);
                request.setAttribute("year", year);
                request.getRequestDispatcher("shippings/report.jsp").forward(request, response);
            }else {
                ArrayList<Integer> count = shippingModel.getReport();
                Calendar cal = Calendar.getInstance();
                request.setAttribute("count", count);
                request.setAttribute("year", cal.get(Calendar.YEAR));
                request.getRequestDispatcher("shippings/report.jsp").forward(request, response);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
